/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tvunetworks.dao.ibatis;

import com.tvunetworks.dao.AccountDao;
import com.tvunetworks.model.Account;
import org.springframework.orm.ObjectRetrievalFailureException;

/**
 *
 * @author michael
 */
public class AccountDaoiBatis extends GenericDaoiBatis<Account, String> implements AccountDao {

    public AccountDaoiBatis() {
        super(com.tvunetworks.model.Account.class);
    }

    public Account getAccount(String username) {
        Account account = (Account) getSqlMapClientTemplate().queryForObject("getAccountByUsername", username);
        if (account == null) {
            logger.warn("uh oh, user not found...");
            throw new ObjectRetrievalFailureException(Account.class, username);
        }
        return account;
    }

    public Account getAccount(String username, String password) {
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        return (Account) getSqlMapClientTemplate().queryForObject("getAccountByUsernameAndPassword", account);
    }

    public void insertAccount(Account account) {
        getSqlMapClientTemplate().insert("insertAccount", account);
        getSqlMapClientTemplate().insert("insertProfile", account);
        getSqlMapClientTemplate().insert("insertSignon", account);
    }

    public void updateAccount(Account account) {
        getSqlMapClientTemplate().update("updateAccount", account);
        getSqlMapClientTemplate().update("updateProfile", account);
        if (account.getPassword() != null && account.getPassword().length() > 0) {
            getSqlMapClientTemplate().update("updateSignon", account);
        }
    }
}
